package projet.metadata;

import projet.CustomException;
import projet.Tools;
import projet.dataaccess.DataAccess;
import projet.dataaccess.PathErrorException;
import projet.dataaccess.XML;

public class Commentaire extends MetaData{
	
	//Fields
	private Integer userId;
	private String text;
	
	/**
	 * Constructeur
	 * @param id
	 * @param userId
	 * @throws CustomException
	 */
	public Commentaire(int id,int userId) throws CustomException{
		super();
		this.id = id;
		this.userId = userId;
		askEdit();
	}
	
	/**
	 * Constructeur
	 * @param d DataAccess de code XML
	 */
	public Commentaire(DataAccess d) throws PathErrorException{
		super();
		this.id = d.getElementInt("id");
		this.userId = d.getElementInt("userId");
		this.text = d.getElementString("text");
	}
	
	/**
	 * askEdit()
	 * Demande la saisie clavier des informations
	 * @throws CustomException
	 */
	public void askEdit() throws CustomException{
	    System.out.print("Votre commentaire : ");
		String buffer = Tools.readLineEdit(this.text);
		if (buffer.length() < 1)
			throw new CustomException("Your comment must have at least have one character !");
		this.text = buffer;
	}
	
	/**
	 * toMediaAccess
	 * Retourne le code XML a écrire
	 * @return DataAccess
	 */
	public DataAccess toMediaAccess() throws PathErrorException {
		DataAccess dataAccess = new XML();
		dataAccess.setName(this.getClass().getSimpleName().toLowerCase());
		dataAccess.addElement("id", this.id.toString());
		dataAccess.addElement("userId", this.userId.toString());
		dataAccess.addElement("text", this.text);
	
		return dataAccess;
	}
	
	/**
	 * Mutateur de id
	 * @param newUserId
	 */
	public void setUserId(int newUserId){
		this.userId = newUserId;
	}
	
	/**
	 * Accesseur de id
	 * @return
	 */
	public int getUserId(){
		return this.userId;
	}
	
	/**
	 * Mutateur de text
	 * @param newText
	 */
	public void setText(String newText){
		this.text = newText;
	}
	
	/**
	 * Accesseur de text
	 * @return
	 */
	public String getText(){
		return this.text;
	}
	
	/**
	 * Méthode toString()
	 */
	public String toString(){
		return "ID: " + this.id + " - USERID: " + this.userId + " - TEXT: " + this.text;
	}
}